package com.apartment.controller;


import com.apartment.auto.JSONAuthentication;
import com.apartment.entity.HouseType;
import com.apartment.mapper.HouseTypeMapper;
import com.apartment.response.Result;
import com.apartment.utils.UpPhotoNameUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author apartment
 * @since 2022-03-13
 */
@RestController
@RequestMapping("/house-type")
public class HouseTypeController extends JSONAuthentication {
    @Autowired
    HouseTypeMapper houseTypeMapper;
    @ApiOperation(value = "添加房间类型")
    @PostMapping("/add")
    public void addHouseType(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String name, @RequestParam("price") float price,@RequestParam("value")String value) throws  Exception{
        HouseType houseType=new HouseType();
        houseType.setName(name);
        houseType.setPrice(price);
        houseType.setValue(value);
        byte[] bytes = file.getBytes();
        String imageFileName = file.getOriginalFilename();
        String fileName = UpPhotoNameUtils.getPhotoName("img",imageFileName);
        Path path = Paths.get("" + fileName);
        Files.write(path, bytes);//写入文件
        String avatar_url=fileName;
        houseType.setImg(avatar_url);
        response.reset();
        int i=houseTypeMapper.insert(houseType);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除房间类型")
    @PostMapping("/del")
    public void delHouseType(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=houseTypeMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改房间类型价格")
    @PostMapping("/updHousePrice")
    public void updScAddress(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("price") float  price)throws Exception{
        HouseType houseType=houseTypeMapper.selectById(id);
        houseType.setPrice(price);
        if(houseTypeMapper.updateById(houseType)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改房间类型介绍")
    @PostMapping("/updHouseValue")
    public void updHouseValue(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("value") String  value)throws Exception{
        HouseType houseType=houseTypeMapper.selectById(id);
        houseType.setValue(value);
        if(houseTypeMapper.updateById(houseType)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询全部房间类型")
    @PostMapping("/findAllHouseType")
    public void findAllHouseType(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<HouseType> shUserPage=new Page<>(current,size);
        Page<HouseType> shUserPages=houseTypeMapper.selectPage(shUserPage,null);
        List<HouseType> records = shUserPages.getRecords();
        int total=houseTypeMapper.selectCount(null);
        if(records.size()>0){
            Result ok = Result.ok().data("shancghuan",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("无上传记录");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询房间类型通过id")
    @PostMapping("/findAllHouseTypeById")
    public void findAllHouseTypeById(HttpServletRequest request, HttpServletResponse response,@RequestParam("id") int id)throws  Exception{
        HouseType houseType = houseTypeMapper.selectById(id);
        if(houseType!=null){
            Result ok = Result.ok().data("housetype",houseType);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("无上传记录");
            this.WriteJSON(request, response, error);
        }
    }
}

